/**
 * Copyright 2016 JustWayward Team
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hzf.booklibrary.ui.easyadapter;

import android.content.Context;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hzf.booklibrary.R;
import com.hzf.booklibrary.base.Constant;
import com.hzf.booklibrary.bean.Recommend;
import com.hzf.booklibrary.bean.book.Book;
import com.hzf.booklibrary.biz.ImageLoader;
import com.hzf.booklibrary.utils.LogUtils;
import com.hzf.booklibrary.view.recyclerview.EasyRecyclerView;
import com.hzf.booklibrary.view.recyclerview.adapter.BaseViewHolder;
import com.hzf.booklibrary.view.recyclerview.adapter.RecyclerArrayAdapter;

/**
 * @author yuyh.
 * @date 2016/9/7.
 */
public class BookListAdapter extends RecyclerArrayAdapter<Book> {
    private ImageLoader imageLoader;
    private static EasyRecyclerView mRecyclerView;
    public BookListAdapter(Context context) {
        super(context);
        imageLoader = ImageLoader.getInstance(8, ImageLoader.Type.FIFO,ImageLoader.NO_CROP,context);
    }

    public static void setRecyclerView(EasyRecyclerView RecyclerView) {
        mRecyclerView = RecyclerView;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(final ViewGroup parent, final int viewType) {
        return new BaseViewHolder<Book>(parent, R.layout.item_book_list) {
            @Override
            public void setData(final Book item) {
                super.setData(item);
                holder.setText(R.id.tv_cover_name, "");//防止布局重用带来的负面影响
                    if(item.icon_path.equals("")){//无封面
                        if(item.filetype.equals("txt")||item.filetype.equals("TXT")){
                            holder.setImageResource(R.id.iv_cover,R.drawable.book_txt_bg );
                        }else if(item.filetype.equals("pdf")||item.filetype.equals("PDF")){
                            holder.setImageResource(R.id.iv_cover,R.drawable.book_pdf_bg );
                        }else {
                            holder.setImageResource(R.id.iv_cover,R.drawable.book_txt_bg );
                        }
                    }else {//有封面
                        String path = Constant.API_BASE_URL+item.icon_path.substring(1,item.icon_path.length());
                        String tag = (String) holder.getView(R.id.iv_cover).getTag();
                        if(!path.equals(tag)){//重新适配，预防闪屏
                            holder.setImageResource(R.id.iv_cover,R.drawable.common_book_bg);
                        }
                        TextView tvCoverName = holder.getView(R.id.tv_cover_name);
                        tvCoverName.setTag(item.icon_path.substring(1,item.icon_path.length()));

                        imageLoader.loadImage(path, (ImageView) holder.getView(R.id.iv_cover),true);
                        LogUtils.e("book_path:"+item.path+"    icon_path:"+item.icon_path);
                        LogUtils.v(path);
                    }
                holder.setText(R.id.tv_book_name, item.name);
                holder.setText(R.id.tv_author,item.author);
                holder.setText(R.id.tv_intro,item.content);
                holder.setText(R.id.tv_update_time,item.updatetime);

                /**
                 * 此处因为布局重用，再次回调调用此方法时，holder的位置已经发生了变化，不一定是你调用此回调方法的位置
                 * 错误示范：//   holder.setText(R.id.tv_cover_name,book.name);
                 * 应采用标记的方式，利用RecyclerView的findViewWithTag找到对应位置的Vie，设置相应的数据
                 *
                 * 注意：在RecyclerView、ListView、GridView中布局重用均会导致这样适配混乱的问题，
                 * 此时应适当采用标记的方法找到对应的View设置相应的数据
                 */
                imageLoader.setOnLoadImageFailedListener(new ImageLoader.OnLoadImageFailedListener() {
                    @Override
                    public void onLoadImageFailed(String tag,ImageView imageView) {
                        for (Book book:getAllData()){
                            if(book.icon_path.equals("")){
                                return;//无封面--防止字符串下标越界异常
                            }
                            if(tag.equals(Constant.API_BASE_URL+book.icon_path.substring(1,book.icon_path.length()))){
                                imageView.setImageResource(R.drawable.book_txt_bg );
                                tag = tag.substring(Constant.API_BASE_URL.length(),tag.length());
                                LogUtils.e(tag);
                                TextView textView = (TextView) mRecyclerView.findViewWithTag(tag);
                                if(textView!=null){
                                    textView.setText(book.name);
                                }
                             //   holder.setText(R.id.tv_cover_name,book.name);
                                break;
                            }
                        }
                    }
                });
            }
        };
    }

}
